﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Windows;
using Caliburn.Micro;
using VirtualKeyboard.Contract;
using VirtualKeyboard.Service.Commands;
using VirtualKeyboard.UI.Localize;

namespace VirtualKeyboard.UI
{
    [PartCreationPolicy(CreationPolicy.Shared)]
    [Export(typeof(ConfigViewModel))]
    public sealed class ConfigViewModel :Screen
    {
        #region Bindings
        private string togglePlayPause;
        private string stop;
        private string muteVolume;
        private string increaseVolume;
        private string decreaseVolume;
        private string nextTrack;
        private string previousTrack;
        private string fullScreen;
        private string forward;
        private string backward;
        private string fastForward;
        private string fastBackward;
        private string veryFastForward;
        private string veryFastBackward;
        private string ok;

        public string TogglePlayPause
        {
            get { return togglePlayPause; }
            set
            {
                togglePlayPause = value;
                base.NotifyOfPropertyChange(() => TogglePlayPause);
            }
        }

        public string IncreaseVolume
        {
            get { return increaseVolume; }
            set
            {
                increaseVolume = value;
                base.NotifyOfPropertyChange(() => IncreaseVolume);
            }
        }

        public string Stop
        {
            get { return stop; }
            set
            {
                stop = value;
                base.NotifyOfPropertyChange(() => Stop);
            }
        }

        public string MuteVolume
        {
            get { return muteVolume; }
            set
            {
                muteVolume = value;
                base.NotifyOfPropertyChange(() => MuteVolume);
            }
        }

        public string DecreaseVolume
        {
            get { return decreaseVolume; }
            set
            {
                decreaseVolume = value;
                base.NotifyOfPropertyChange(() => DecreaseVolume);
            }
        }

        public string NextTrack
        {
            get { return nextTrack; }
            set
            {
                nextTrack = value;
                base.NotifyOfPropertyChange(() => NextTrack);
            }
        }

        public string PreviousTrack
        {
            get { return previousTrack; }
            set
            {
                previousTrack = value;
                base.NotifyOfPropertyChange(() => PreviousTrack);
            }
        }

        public string FullScreen
        {
            get { return fullScreen; }
            set
            {
                fullScreen = value;
                base.NotifyOfPropertyChange(() => FullScreen);
            }
        }

        public string Forward
        {
            get { return forward; }
            set
            {
                forward = value;
                base.NotifyOfPropertyChange(() => Forward);
            }
        }

        public string Backward
        {
            get { return backward; }
            set
            {
                backward = value;
                base.NotifyOfPropertyChange(() => Backward);
            }
        }

        public string FastForward
        {
            get { return fastForward; }
            set
            {
                fastForward = value;
                base.NotifyOfPropertyChange(() => FastForward);
            }
        }

        public string FastBackward
        {
            get { return fastBackward; }
            set
            {
                fastBackward = value;
                base.NotifyOfPropertyChange(() => FastBackward);
            }
        }

        public string VeryFastForward
        {
            get { return veryFastForward; }
            set
            {
                veryFastForward = value;
                base.NotifyOfPropertyChange(() => VeryFastForward);
            }
        }

        public string VeryFastBackward
        {
            get { return veryFastBackward; }
            set
            {
                veryFastBackward = value;
                base.NotifyOfPropertyChange(() => VeryFastBackward);
            }
        }

        //public string OK
        //{
        //    get { return ok; }
        //    set
        //    {
        //        ok = value;
        //        OnPropertyChanged("OK");
        //        base.NotifyOfPropertyChange(() => OK);
        //    }
        //}
        #endregion
        
        private readonly MediaMethodConfig mediaMethodConfig;

        public ConfigViewModel()
        {
            DisplayName = AppResources.ShortcutKeyConfig;

            mediaMethodConfig = MediaMethodConfig.Instance;
            
            togglePlayPause = mediaMethodConfig.Parse(KeyMethods.Media.TogglePlayPause);
            stop = mediaMethodConfig.Parse(KeyMethods.Media.Stop);
            muteVolume = mediaMethodConfig.Parse(KeyMethods.Media.MuteVolume);
            increaseVolume = mediaMethodConfig.Parse(KeyMethods.Media.IncreaseVolume);
            decreaseVolume = mediaMethodConfig.Parse(KeyMethods.Media.DecreaseVolume);
            nextTrack = mediaMethodConfig.Parse(KeyMethods.Media.NextTrack);
            previousTrack = mediaMethodConfig.Parse(KeyMethods.Media.PreviousTrack);
            fullScreen = mediaMethodConfig.Parse(KeyMethods.Media.FullScreen);
            forward = mediaMethodConfig.Parse(KeyMethods.Media.Forward);
            backward = mediaMethodConfig.Parse(KeyMethods.Media.Backward);
            fastForward = mediaMethodConfig.Parse(KeyMethods.Media.FastForward);
            fastBackward = mediaMethodConfig.Parse(KeyMethods.Media.FastBackward);
            veryFastForward = mediaMethodConfig.Parse(KeyMethods.Media.VeryFastForward);
            veryFastBackward = mediaMethodConfig.Parse(KeyMethods.Media.VeryFastBackward);
            ok = mediaMethodConfig.Parse(KeyMethods.Keyboard.Enter);
        }

        public void OnSave()
        {
            mediaMethodConfig.UpdateKey(KeyMethods.Media.TogglePlayPause, TogglePlayPause);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.Stop, Stop);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.MuteVolume, MuteVolume);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.IncreaseVolume, IncreaseVolume);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.DecreaseVolume, DecreaseVolume);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.NextTrack, NextTrack);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.PreviousTrack, PreviousTrack);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.FullScreen, FullScreen);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.Forward, Forward);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.Backward, Backward);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.FastForward, FastForward);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.FastBackward, FastBackward);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.VeryFastForward, VeryFastForward);
            mediaMethodConfig.UpdateKey(KeyMethods.Media.VeryFastBackward, veryFastBackward);
            //mediaMethodConfig.UpdateKey(KeyMethods.Keyboard.Enter, OK);
            TryClose(true);
        }

        public void OnCancel()
        {
            TryClose(false);
        }
    }
}
